/********************************************************************************
* File Name:	manageplaylist.cpp
* Description:	第12章实例训练          
* Reference book:《Linux环境下Qt4图形界面与MySQL编程》，机械工业出版社.2012.1  
* E_mail: openlinux2011@gmail.com		                
*
********************************************************************************/
#include "manageplaylist.h"
ManagePlayList::ManagePlayList():originListCount(0)
{
    obtainHistoryPlayList();
}
ManagePlayList::~ManagePlayList()
{
    updateHistoryPlayList();
}
void ManagePlayList::obtainHistoryPlayList()//获得历史播放列表内容
{
    QFile file("playlist.txt");//此文件用于存储播放列表中对应文件的绝对路径
    if(!file.open(QIODevice::ReadOnly | QIODevice::Text))
        return;
    while (!file.atEnd()) //将playlist.txt文件中保存的绝对路径加入playList，并计数
    {
        QString line = file.readLine();
        playList.push_back(line.mid(0,line.length()-1));
        originListCount++;
    }
}
void ManagePlayList::updateHistoryPlayList()//更新历史播放列表内容
{
    QFile file("playlist.txt");
    if (!file.open(QIODevice::WriteOnly |QIODevice::Append| QIODevice::Text))
        return;
    QTextStream out(&file);
    for(int i = originListCount; i < playList.count(); i++) //将playList数组中的内容重写入文件playlist.txt中
    {
        out << playList[i] << "\n";
    }
}
QString ManagePlayList::getOneFilePath(qint32 index) //获得与index对应的文件的绝对路径，index与播放列表中的行号对应
{
    return playList[index];
}
void ManagePlayList::obtainCurrentFilePath(QString currentFilePath)//将本次播放的文件的路径存入到playList中
{
    playList.push_back(currentFilePath); //加入数组最后
}
qint32 ManagePlayList::indexOf(QString filePath) //获取filePath对应文件的索引，此索引与播放列表中的行号对应
{
    return playList.indexOf(filePath);
}
qint32 ManagePlayList::getOriginListCount() //返回当前播放列表中文件的个数
{
    return playList.count();
}
void ManagePlayList::clear() //清空容器
{
    playList.clear(); //清空数组
    originListCount = 0;
    QFile file("playlist.txt"); //清空文件
    if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
        return;
}
